<?php

namespace App\Http\Controllers\Api\Common;

use App\Http\Controllers\Controller;
use App\Http\Resources\Common\WeatherResource;
use App\Trip\Train\TrainQueryFromToData;
use App\Trip\Weather\Amap\Weather;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;

class WeatherController extends Controller
{
    /**
     * @throws Exception
     * @throws GuzzleException
     */
    public function weather(Request $request)
    {
        $weather = new Weather();

        $queryData = new TrainQueryFromToData([
            'code' => $request->input('code', 650100),
            'type' => $request->input('type', 0),
        ]);

        $cityData                = $queryData->parse2City();
        $data                    = $weather->weatherInfo($cityData['code']);
        $ret                     = $data[0] ?? [];
        $ret['weather_uy']       = trans2uy("weather.{$ret['weather']}");
        $ret['winddirection_uy'] = trans2uy("weather.{$ret['winddirection']}");
        $ret['weather_type']     = get_weather_type($ret['weather']);
        $ret['weather_icon']     = get_weather_icon($ret['weather']);
        return $this->success('', new WeatherResource($ret));
    }
}
